package kayao.client.data;

import kayao.client.network.message.KingRequestMessage;
import kayao.client.network.message.MessageHandler;
import kayao.client.network.message.SubscriptionRequestMessage;
import kayao.common.KayaoValues;

/*
 * 	Sends packets to kings to inform that still alive and current status.
 * 	Retrieves packets from kings in shared memory, put by ListeningPeersThread
 * 	and processes.
 */
public class Slave {
	
//	public static int slaveSendCounter = 0;
//	public static int slaveSendToResponseCounter = 0;
	

	//	private static String[] mKings = new String[KayaoValues.NUMBER_OF_KINGDOMS];
	private static String[] mKings = new String[KayaoValues.NUMBER_OF_KINGDOMS];
	
	// My kings are kept in a list
	public static String[] getMyKings(){
		return mKings;
	}

	public static String getKingByID(int i){
		return mKings[i];
	}

	public static void printKingInfo(){
		System.out.println("!Slave.printKingInfo");
		for(int i=0; i<mKings.length; i++){
			System.out.println("!region "+i+" king: "+mKings[i]);
		}
		System.out.println("!-----------------------");
	}
	
	public static boolean updateMyKings(String[] kings){
		boolean changes=false;
		for(int i = 0; i<mKings.length; i++){
			if(mKings[i]==null && kings[i]==null)
				continue;
			else if(mKings[i]!=null && kings[i]==null)
			{
				changes=true;
				break;
			}
			else if(mKings[i]==null && kings[i]!=null)
			{
				changes=true;
				break;
			}
			else if(!mKings[i].equals(kings[i])){
				changes=true;
				break;
			}
		}
		mKings = kings;
		return changes;
	}

	public static void addMyKing(String mKingIp, int kingdom){
		mKings[kingdom] = mKingIp;
		if(mKingIp.equals(SharedMemory.getInstance().getMyself().getIP()))
			SharedMemory.getInstance().setKing(new King((byte)kingdom));
	}

	public static void removeMyKing(String kingIp){
		for(int i = 0; i<mKings.length; i++){
			if(mKings[i]!=null && mKings[i].equals(kingIp)){
				mKings[i]=null;
				if(kingIp.equals(SharedMemory.getInstance().getMyself().getIP()))
					SharedMemory.getInstance().deleteKing();
				break;
			}
		}
		if(!hasKing()){
			System.out.println("Slave: removeMyKing - new king req");
			newKingRequest();
		}
	}

	public static boolean hasKing(){
		for(int i = 0; i<mKings.length; i++){
			if(mKings[i] != null)
				return true;
		}
		return false;
	}
	
	public static boolean isKing(){
		for(int i = 0; i<mKings.length; i++){
			if(mKings[i]==null)
				continue;
			if(mKings[i].equals(SharedMemory.getInstance().getMyself().getIP()))
				return true;
		}
		return false;
	}

	public static boolean isKingInRegion(int region){
		if(mKings[region]==null){
			return false;
		}
		else if(mKings[region].equals(SharedMemory.getInstance().getMyself().getIP())){
			return true;
		} else {			
			return false;
		}
	}

	
	public static void newKingRequest(){
		MessageHandler.insertOutgoingMessage(new KingRequestMessage(KayaoValues.SERVER_IP));
	}

	// Insert messages to send to Kings
	public static void send(){
		int i=0;
		for(String ip:mKings){
			if(ip==null){
				i++;
				continue;
			}
			if(!ip.equals(SharedMemory.getInstance().getMyself().getIP())) {	// Don't send to myself...
				MessageHandler.insertOutgoingMessage(new SubscriptionRequestMessage(ip,2000,i));
			}
			i++;
		}
//		slaveSendCounter++;
	}

	public static void sendToResponse(String ip, int kingdom){
		if(!ip.equals(SharedMemory.getInstance().getMyself().getIP())) {
			// Don't send to myself...
			MessageHandler.insertAwaitingResponseMessage(new SubscriptionRequestMessage(ip,5000, kingdom));
		}
//		slaveSendToResponseCounter++;
	}

	public static int getKingdomFromIP(String ip) {
		for(int i = 0; i<mKings.length; i++){
			if(mKings[i]!=null && mKings[i].equals(ip)){
				return i;
			}
		}
		return -1;
	}
}
